<?php

# Wish handling library

function show_link($wish) {
    echo itemstats_parse("[item]" . $wish["item_id"] . "[/item]").' <a href="item-view.php?id='.$wish["item_id"].'">item</a><br/>';
  }

function show_pure($wish) {
    echo itemstats_parse("[item]" . $wish["item_id"] . "[/item]");
}

function show_wish($wish, $src, $sys) {
    show_link($wish);
    for ($i = bid_max(); $i >= 0; --$i) {
        if ($wish["wish_level"] == $i) {
            echo " <b>".bid2text($i)."</b> ";
        } else {
            echo '<a href="make-wish.php?id='.$wish["item_id"].'&level='.$i.'&src='.$src.'">'.bid2text($i).'</a> ';
        }
    }
    echo "<br/>\n";
}

# Show a HTML blob for an item
#  [item]
#  <bid list>
#  drops from <boss list>
function show_item($item, $character, $db, $src, $sys=0, $boss_list=1) {
    $sql = "select wish_item as item_id, wish_character, wish_level
            from wm_wishes 
            where wish_item = '".$item["item_id"]."' and 
                  wish_character='".$character["character_id"]."'";
    $result = $db->query($sql);
    $wish = $db->fetch_assoc($result);
    if($wish["wish_level"]>0) {
        // Found a character's item wish
        show_wish($wish, $src, $sys);
    }
    else {
        // No wish for this character on this item, pass $item
        show_wish($item, $src, $sys);
    }
    if($boss_list == 1) {
        $sql = "select boss_id, boss_name
                from wm_bosses, wm_items 
                where wm_items.item_id = ".$item["item_id"]." and 
                      wm_items.item_boss = wm_bosses.boss_id";
        $bosses = $db->query($sql);
        $boss = $db->fetch_assoc($bosses);
        echo 'Drops from <a href="boss.php?id='.$boss["boss_id"].'">'.$boss["boss_name"]."</a>\n";
        while($boss = $db->fetch_assoc($bosses)) {
            echo ', <a href="boss.php?id='.$boss["boss_id"].'">'.$boss["boss_name"]."</a>\n";
        }
    }
}

# Translate bid_level to it's text counterpart
function bid2text($bid) {
    $text = array(5 => "Max", 4 => "Mod", 3 => "Min", 2 => "Micro", 1 => "Off spec", 0 => "Non");
    if (array_key_exists($bid, $text)) {
        return $text[$bid];
    }
    return "Unknown bid: ".$bid.". Constact administrators.";
}

# Return the max level of bids. Sentral dummy method to prevent hardcoded values to leak into
# scripts
function bid_max() {
    return 5;
}

# Return the percentage a bid_level represent. This percentage is how much of your DKP pool
# you bid on an item.
function bid2perc($bid) {
    $perc = array (5 => 1, 4 => 0.6, 3 => 0.2, 2 => 0.05, 1 => 0);
    if ($bid <= 5 and $bid >= 1) {
        return $perc[$bid];
    } else {
        die("Unknown bid_level: '".$bid."', please contact an administrator.");
    }
}

# Dummy function that returns the minimum a bid can be
function floor_min() {
    return 0.5;
}

?>
